package com.itcast.rediscache.util;

import cn.hutool.core.util.StrUtil;

import java.net.InetAddress;

/**
 * @author zhangtian105
 * @ClassName InetAddressUtil.java
 * @projectName Redis
 * @Description IP工具类
 * @createTime 2020年11月18日 21:26:00
 */
public class InetAddressUtil {
    private InetAddressUtil() {
    }

    private static String localServerIp = null;

    /*public static String getRemoteIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (isNotIp(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (isNotIp(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (isNotIp(ip)) {
            ip = request.getRemoteAddr();
        }
        if (isIp(ip)) {
            ip = Pattern.compile(",").split(ip)[0];
        }
        return ip;
    }*/

    /**
     * 判断ip地址是正确的
     *
     * @param ip
     *         ip地址
     * @return true-是，false-否
     */
    private static boolean isIp(String ip) {
        return !isNotIp(ip);
    }

    /**
     * 判断ip地址是不正确的
     *
     * @param ip
     *         ip地址
     * @return true-是，false-否
     */
    private static boolean isNotIp(String ip) {
        return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip);
    }

    public static long ipToLong(String ip) {
        int firstDot = ip.indexOf('.');
        int secDot = ip.indexOf('.', firstDot + 1);
        int thirdDot = ip.indexOf('.', secDot + 1);

        if (-1 == firstDot || -1 == secDot || -1 == thirdDot) {
            throw new IllegalArgumentException("illegal argument");
        }

        String firstStr = ip.substring(0, firstDot);
        String secStr = ip.substring(firstDot + 1, secDot);
        String thirdStr = ip.substring(secDot + 1, thirdDot);
        String fourthStr = ip.substring(thirdDot + 1, ip.length());

        long firstLong = Long.parseLong(firstStr);
        firstLong = firstLong << 24;

        long secLong = Long.parseLong(secStr);
        secLong = secLong << 16;

        long thirdLong = Long.parseLong(thirdStr);
        thirdLong = thirdLong << 8;

        long fourthLong = Long.parseLong(fourthStr);

        return firstLong + secLong + thirdLong + fourthLong;
    }

    public static String longToIp(long ipLong) {
        long temp = 0;
        long first = ipLong / (1 << 24);
        temp = ipLong % (1 << 24);

        long sec = temp / (1 << 16);
        temp = temp % (1 << 16);

        long third = temp / (1 << 8);
        temp = temp % (1 << 8);

        long fourth = temp;

        StringBuilder sb = new StringBuilder();
        sb.append(first).append(".").append(sec).append(".").append(third).append(".").append(fourth);

        return sb.toString();
    }

    public static String getLocalServerIp() {
        try {
            if(StrUtil.isNotBlank(localServerIp)) {
                return localServerIp;
            } else {
                InetAddress address = InetAddress.getLocalHost();
                localServerIp = address.getHostAddress();
                return localServerIp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
